package com.example.windowmanager;

import android.content.Context;
import android.graphics.Color;
import android.os.Build;
import android.view.Gravity;
import android.view.MotionEvent;
import android.view.View;
import android.view.WindowManager;
import android.widget.TextView;

import static android.content.Context.WINDOW_SERVICE;

public class FloatWindowUtil {

    private WindowManager.LayoutParams params = null;
    private WindowManager wm = null;
    private TextView textView = null;
    private boolean isShow = false;
    private volatile static FloatWindowUtil instance;

    private FloatWindowUtil() {
    }

    public static FloatWindowUtil getInstance() {
        if (instance == null) {
            synchronized (FloatWindowUtil.class) {
                if (instance == null) {
                    instance = new FloatWindowUtil();
                }
            }
        }
        return instance;
    }

    public void show(Context context, String str) {

        if (wm == null) {
            wm = (WindowManager) context.getSystemService(WINDOW_SERVICE);
        }

        if (params == null) {
            params = new WindowManager.LayoutParams();
//            params.type = WindowManager.LayoutParams.TYPE_SYSTEM_ERROR;
//            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {//6.0
//                params.type = WindowManager.LayoutParams.TYPE_SYSTEM_ERROR;
//            } else {
//                params.type = WindowManager.LayoutParams.TYPE_SYSTEM_ALERT;
//            }
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
                params.type = WindowManager.LayoutParams.TYPE_APPLICATION_OVERLAY;
            } else {
                params.type = WindowManager.LayoutParams.TYPE_PHONE;
            }

            params.flags = WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED |
                    WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN |
                    WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS |
                    WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL |
                    WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE;
//            params.format = PixelFormat.RGBX_8888;
            params.width = WindowManager.LayoutParams.MATCH_PARENT;
            params.height = WindowManager.LayoutParams.WRAP_CONTENT;
            params.gravity = Gravity.CENTER;
        }

        if (textView == null) {
            textView = new TextView(context);
            textView.setPadding(10, 4, 10, 4);
            textView.setBackgroundColor(Color.rgb(50,50,200));
            textView.setTextColor(Color.WHITE);
            textView.setOnTouchListener(new FloatingOnTouchListener());
            textView.setGravity(Gravity.CENTER);
            wm.addView(textView, params);
            isShow = true;
        }
        textView.setText(str);
    }

    public void hide() {
        if (isShow) {
            if (wm != null) {
                wm.removeView(textView);
                textView = null;
                isShow = false;
            }
        }
    }


    class FloatingOnTouchListener implements View.OnTouchListener {
        private int x;
        private int y;

        @Override
        public boolean onTouch(View view, MotionEvent event) {
            switch (event.getAction()) {
                case MotionEvent.ACTION_DOWN:
                    x = (int) event.getRawX();
                    y = (int) event.getRawY();
                    break;
                case MotionEvent.ACTION_MOVE:
                    int nowX = (int) event.getRawX();
                    int nowY = (int) event.getRawY();
                    int movedX = nowX - x;
                    int movedY = nowY - y;
                    x = nowX;
                    y = nowY;
                    params.x = params.x + movedX;
                    params.y = params.y + movedY;
                    // 更新悬浮窗控件布局
                    wm.updateViewLayout(view, params);
                    break;
                default:
                    break;
            }
            return false;
        }
    }

}



